package org.fmi.lje.main;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.fmi.lje.Main;
import org.fmi.lje.loader.ProjectLoader;
import org.fmi.lje.model.UmlGenProject;
import org.fmi.lje.pp.ProjectCompiler;
import org.fmi.lje.uml.Diagram;
import org.fmi.lje.uml.DiagramBuilder;

import com.google.inject.Inject;

public class ProjectExecutor {
	
	private static final Logger log = Logger.getLogger(ProjectExecutor.class);

	private ProjectCompiler projectCompiler;
	private ProjectLoader projectLoader;
	private DiagramBuilder diagramBuilder;

	@Inject
	public ProjectExecutor(ProjectCompiler projectCompiler, ProjectLoader projectLoader, DiagramBuilder diagramBuilder) {
		super();
		this.projectCompiler = projectCompiler;
		this.projectLoader = projectLoader;
		this.diagramBuilder = diagramBuilder;
	}

	public List<Diagram> execute(UmlGenProject project) {
		List<Diagram> diagrams = new ArrayList<Diagram>();
		
		projectCompiler.compileProject(project);
		ClassLoader cl = projectLoader.loadProject(project);
		
		for(String targetClass : project.getDiagramClasses().getEntries()) {
			try {
				Class<?> clazz = cl.loadClass(targetClass);
				
				Diagram diagram = diagramBuilder.buildDiagram(clazz);
				
				diagrams.add(diagram);
			} catch (ClassNotFoundException e) {
				log.info("Could not load class: " + targetClass);
			}
		}
		
		return diagrams;
	}
}
